/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.service.impl;

import org.smslib.AGateway;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.http.ClickatellHTTPGateway;
import org.springframework.stereotype.Service;

import com.opgea.token.service.QmsSMS;

/**
 * 
 * @author Ramesh
 */
@Service
public class QmsSMSImpl implements QmsSMS {

    @Override
    public void sendTokenNo(String mobileNo, String tokenNo) throws Exception {
	OutboundMessage msg;
	OutboundNotification outboundNotification = new OutboundNotification();
	System.out
		.println("Example: Send message from Clickatell HTTP Interface.");
	System.out.println(Library.getLibraryDescription());
	System.out.println("Version: " + Library.getLibraryVersion());
	ClickatellHTTPGateway gateway = new ClickatellHTTPGateway(
		"clickatell.http.1", "3354665", "shekharkumargupta",
		"superpower1070");
	gateway.setOutbound(true);
	org.smslib.Service.getInstance().setOutboundMessageNotification(
		outboundNotification);
	// Do we need secure (https) communication?
	// True uses "https", false uses "http" - default is false.
	gateway.setSecure(true);
	org.smslib.Service.getInstance().addGateway(gateway);
	org.smslib.Service.getInstance().startService();
	// Create a message.
	msg = new OutboundMessage(mobileNo, tokenNo);
	msg.setFrom("SMSLIB.ORG");
	// Ask for coverage.
	System.out.println("Is recipient's network covered? : "
		+ gateway.queryCoverage(msg));
	// Send the message.
	org.smslib.Service.getInstance().sendMessage(msg);
	System.out.println(msg);
	// Now query the service to find out our credit balance.
	System.out.println("Remaining credit: " + gateway.queryBalance());
	// Send some messages in async mode...
	// After this, your IOutboundMessageNotification method will be called
	// for each message sent out.
	// msg = new OutboundMessage("+30...", "Max");
	// srv.queueMessage(msg, "clickatell.http.1",
	// AbstractGateway.Priority.HIGH);
	// msg = new OutboundMessage("+30...", "Min");
	// srv.queueMessage(msg, "clickatell.http.1",
	// AbstractGateway.Priority.LOW);
	// System.out.println("Now Sleeping - Hit <enter> to terminate.");
	// System.in.read();
	org.smslib.Service.getInstance().stopService();
    }

    public class OutboundNotification implements IOutboundMessageNotification {
	public void process(AGateway gateway, OutboundMessage msg) {
	    System.out.println("Outbound handler called from Gateway: "
		    + gateway.getGatewayId());
	    System.out.println(msg);
	}
    }

}
